package com.egao.orderB.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.CustomerBiz;
import com.egao.orderB.biz.CustomerMaintainFeeAttachBiz;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.CustomerMaintainFeeAttach;
import com.egao.orderB.service.CustomerMaintainFeeAttachService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

//import com.egao.orderB.mapper.CustomerMaintainFeeAttachMapper;
//import javax.transaction.Transactional;

/**
 * 货代附加费维护服务实现类
 * @author : Doman
 * @date : 2021-07-15 12:46:52
 * @version ：1.0
 */
@Service
public class CustomerMaintainFeeAttachServiceImpl /*extends ServiceImpl<CustomerMaintainFeeAttachMapper, CustomerMaintainFeeAttach> */implements CustomerMaintainFeeAttachService {

	@Autowired
	private CustomerMaintainFeeAttachBiz customerMaintainFeeAttachBiz;
    @Autowired
    private CustomerBiz customerBiz;

    /**
    @Override
    public PageResult<CustomerMaintainFeeAttach> listPage(PageParam<CustomerMaintainFeeAttach> page) {
        List<CustomerMaintainFeeAttach> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<CustomerMaintainFeeAttach> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser)  throws Exception{
        return customerMaintainFeeAttachBiz.list(page,customerMaintainFeeAttach,loginUser);
    }

    @Override
    public JsonResult list(CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) {
        return JsonResult.ok().setData(customerMaintainFeeAttachBiz.list(customerMaintainFeeAttach,loginUser));
    }

    @Override
    public JsonResult getById(CustomerMaintainFeeAttach customerMaintainFeeAttach) {
        return JsonResult.ok().setData(customerMaintainFeeAttachBiz.getById(customerMaintainFeeAttach));
    }

    @Transactional
    @Override
    public JsonResult save(CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) {
        if (customerMaintainFeeAttachBiz.save(customerMaintainFeeAttach, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) {
        if (customerMaintainFeeAttachBiz.update(customerMaintainFeeAttach, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(CustomerMaintainFeeAttach customerMaintainFeeAttach, User loginUser) {
        if (customerMaintainFeeAttachBiz.remove(customerMaintainFeeAttach, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Transactional
    @Override
    public JsonResult importBatch(MultipartFile file, User loginUser) {
//        StringBuilder sb = new StringBuilder();
        try {
            // 读取excel
            int startRow = 1;
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream(), 0);
            List<List<Object>> list = reader.read(startRow);
            // 进行非空和重复检查
//            sb.append(CoreUtil.excelCheckBlank(list, startRow, 0, 1, 2, 3, 4));
//            sb.append(CoreUtil.excelCheckRepeat(list, startRow, 0, 1, 2, 3));
//            if (!sb.toString().isEmpty()) return JsonResult.error(sb.toString());
            // 进行数据库层面检查
            int okNum = 0, errorNum = 0;
            for (int i = 0; i < list.size(); i++) {
                List<Object> objects = list.get(i);
                String customerName = String.valueOf(objects.get(0));  // 客户
                String feeName = String.valueOf(objects.get(1));  // 费用名称
                String boxType = String.valueOf(objects.get(2));  // 柜型
                String price = String.valueOf(objects.get(3));  // 价格
                Customer c1 = new Customer();
                c1.setName(customerName);
                List<Customer> customers = customerBiz.getByName(c1);
                if(customers != null && customers.size() == 1) {
                    c1 = customers.get(0);
                    CustomerMaintainFeeAttach customerMaintainFee = new CustomerMaintainFeeAttach();
                    customerMaintainFee.setCustomerId(Integer.valueOf(c1.getId()));
                    customerMaintainFee.setName(feeName);
                    customerMaintainFee.setBoxType(boxType);
                    customerMaintainFee.setPrice(price);
                    customerMaintainFeeAttachBiz.save(customerMaintainFee,loginUser);
                    okNum++;
                }
            }
            return JsonResult.ok("导入完成，成功" + okNum + "条，失败" + errorNum + "条");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JsonResult.error("导入失败");
    }

}
